package com.nervenets.web.controller.knowledge;

import com.nervenets.utils.StringUtils;
import com.nervenets.web.hibernate.domain.knowledge.Knowledge;
import com.nervenets.web.hibernate.domain.knowledge.Tags;
import com.nervenets.web.redis.GlobalService;
import com.nervenets.web.redis.domain.user.UserStored;
import lombok.Data;

import java.io.Serializable;
import java.util.Set;

@Data
public class KnowledgeDto implements Serializable {
    private long id;
    private int createTime;
    private long userId;
    private String userName;
    private String userAvatar;
    private Set<Tags> tags;
    private String title;
    private String content;

    public KnowledgeDto(Knowledge knowledge, GlobalService globalService) {
        this.id = knowledge.getId();
        this.createTime = knowledge.getCreateTime();
        this.userId = knowledge.getUserId();
        if (this.userId > 0) {
            final UserStored user = globalService.getUser(this.userId);
            if (null != user) {
                this.userName = user.getExtra().getWxAvatar();
                this.userAvatar = user.getExtra().getWxName();
            }
        }
        this.tags = knowledge.getTags();
        this.title = knowledge.getTitle();
        this.content = StringUtils.clearHtmlTags(knowledge.getContent());
    }
}
